﻿using System;

namespace XSerialization
{
    /// <summary>
    /// Custom attribute to map between the XML schema system and the common language runtime.
    /// </summary>
    public interface IXBinding
    {
        /// <summary>
        /// Name of the Xml Element
        /// </summary>
        string XmlName { get; set; }

        /// <summary>
        /// Xml Element is a Attribute?
        /// </summary>
        bool IsAttribute { get; set; }

        /// <summary>
        /// Is the Xml Element required?
        /// </summary>
        bool IsRequired { get; set; }

        /// <summary>
        /// Index of Element in the xml sequence
        /// </summary>
        uint SequenceIndex { get; set; }

        /// <summary>
        /// Specific Type
        /// </summary>
        Type ObjectType { get; set; }
    }

    /// <summary>
    /// Custom attribute to map between the XML schema system and the common language runtime. 
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public abstract class XBinding : Attribute, IXBinding
    {
        /// <summary>
        /// Name of the Xml Element
        /// </summary>
        public string XmlName { get; set; }

        /// <summary>
        /// Xml Element is a Attribute?
        /// </summary>
        public bool IsAttribute { get; set; }

        /// <summary>
        /// Is the Xml Element required?
        /// </summary>
        public bool IsRequired { get; set; }

        /// <summary>
        /// Index of Element in the xml sequence
        /// </summary>
        public uint SequenceIndex { get; set; }

        /// <summary>
        /// Specific Type
        /// </summary>
        public Type ObjectType { get; set; }

        /// <summary>
        /// Ctor for simple Objects like string, class or enum.
        /// </summary>
        /// <param name="xmlName">Name of the Xml Element</param>
        /// <param name="isAttribute">Xml Element is a Attribute?</param>
        /// <param name="isRequired">Is the Xml Element required?</param>
        /// <param name="sequenceIndex">Index of Element in the xml sequence</param>
        protected XBinding(string xmlName, bool isAttribute, bool isRequired, uint sequenceIndex) : this(xmlName, isAttribute, isRequired, sequenceIndex, null) { }

        /// <summary>
        /// Ctor for complex Objects like IList or Interfaces
        /// </summary>
        /// <param name="xmlName">Name of the Xml Element</param>
        /// <param name="isAttribute">Xml Element is a Attribute?</param>
        /// <param name="isRequired">Is the Xml Element required?</param>
        /// <param name="sequenceIndex">Index of Element in the xml sequence</param>
        /// <param name="objectType">Specific Type</param>
        protected XBinding(string xmlName, bool isAttribute, bool isRequired, uint sequenceIndex, Type objectType)
        {
            XmlName = xmlName;
            IsAttribute = isAttribute;
            IsRequired = isRequired;
            SequenceIndex = sequenceIndex;
            ObjectType = objectType;
        }
    }

}
